<?php

namespace app\api\model\wanlshop;

use think\Model;
use traits\model\SoftDelete;
use think\exception\DbException;
use think\db\exception\DataNotFoundException;
use think\db\exception\ModelNotFoundException;

class Pay extends Model
{
    use SoftDelete;

    // 表名
    protected $name = 'wanlshop_pay';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = 'deletetime';

    //写入 order_pay_no
    protected function setPayNoAttr($value)
    {
        // 高并发有几率重复，暂时不需要
        return date('YmdHis') . substr(strrev($value), -8) . substr(implode(NULL, array_map('ord', str_split(substr(uniqid(), 7, 13), 1))), 0, 8);
    }

    /**
     * 是否全款购买
     * @param $oid
     * @param $uid
     * @return int
     * @throws DataNotFoundException
     * @throws ModelNotFoundException
     * @throws DbException
     */
    public static function isFullayment($oid, $uid)
    {
        $info = self::where(['order_id' => $oid, 'user_id' => $uid])->find();
        return intval($info['coupon_price'] == 0 && $info['score_price'] == 0 && $info['discount_price'] == 0 &&
            $info['system_price'] == 0 && $info['invoice_price'] == 0 && $info['oldstaff_price'] == 0);
    }
}